package com.springcloud.zuul.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.springcloud.zuul.common.constant.ServiceCode;
import com.springcloud.zuul.common.utlis.ConvertDateTime;
import com.springcloud.zuul.entity.SysMenuEntity;
import com.springcloud.zuul.entity.SysMenusEntity;
import com.springcloud.zuul.entity.SysProductMenuEntity;
import com.springcloud.zuul.exception.ServiceException;
import com.springcloud.zuul.mapper.SysProductMenuDao;
import com.springcloud.zuul.service.SysMenuService;
import com.springcloud.zuul.service.SysProductMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: springcloud-zuul
 * @description: 产品与权限关联表
 * @author: zhlu
 * @create: 2020-01-02 11:51
 * @Introduction(说明背景故事)
 **/
@Service("sysProductMenuService")
@Slf4j
public class SysProductMenuServiceImpl extends ServiceImpl<SysProductMenuDao, SysProductMenuEntity> implements SysProductMenuService {

    @Autowired
    SysMenuService sysMenuService;

    /**
     * Description:保存与更新产品与权限关联表数据
     * @param productId
     * @param menuList
     * @return void
     * @author zhlu
     * @date 2020/1/2 11:50 上午
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(String productId,String productName, List<String> menuList) {
        //先删除产品与权限功能关联数据
        deleteBatch(Lists.newArrayList(productId));

        if(menuList.size() == 0){
            return ;
        }
        //保存产品与权限菜单的关联表数据
        for(String str:menuList){
            SysMenuEntity sysMenuEntity=sysMenuService.queryByMenuId(str);
            if(Objects.isNull(sysMenuEntity)){
                throw new ServiceException(ServiceCode.PRODUCT_MENU_EXISTED);
            }
            SysProductMenuEntity productMenuEntity=new SysProductMenuEntity();
            productMenuEntity.setProductId(productId);
            productMenuEntity.setProductName(productName);
            productMenuEntity.setMenuId(str);
            productMenuEntity.setMenuName(sysMenuEntity.getMenuName());
            productMenuEntity.setInstm(ConvertDateTime.getCurrentTime());
            productMenuEntity.setInsfnc(this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());
            //保存产品与权限菜单数据
            this.save(productMenuEntity);
        }

    }

    /**
     * Description:根据产品编码，批量删除产品与权限菜单关联表数据
     * @author zhlu
     * @date 2020/1/2 2:01 下午
     * @param productIds
     * @return int
     */
    @Override
    public int deleteBatch(List<String> productIds){
        return baseMapper.deleteBatch(productIds);
    }

    /**
     * Description:根据产品编码获取产品权限功能列表
     * @author zhlu
     * @date 2020/1/6 10:15 上午
     * @param productId
     * @return java.util.List<java.lang.String>
     */
    @Override
    public List<String>queryMenuIdList(String productId){
        return baseMapper.queryMenuIdList(productId);
    }
}
